//
//  ScrollTextView.m
//  nobalmetal
//
//  Created by mac on 2017/1/5.
//  Copyright © 2017年 judu. All rights reserved.
//

#import "ScrollTextView.h"


@interface ScrollTextView ()

@property (nonatomic,strong) UILabel *textLabelOne;
@property (nonatomic,strong) UILabel *textLabelTwo;

@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) NSInteger index;

@property (nonatomic,assign) NSInteger duration;

@end


@implementation ScrollTextView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.textLabelOne];
        [self addSubview:self.textLabelTwo];
        // 裁剪
        self.clipsToBounds = YES;
    }
    return self;
}


#pragma mark - public method

-(void)startAnimate{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(repeadFuntion) userInfo:self repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    // 相当于 调用一次 [self repeadFuntion];
    [self.timer fire];
}

-(void)endAnimate{
    [self.timer invalidate];
}


-(void)setInfoLabelFont:(UIFont *)font textColor:(UIColor *)textColor{
    
    self.textLabelOne.font = font;
    self.textLabelOne.textColor = textColor;
    
    self.textLabelTwo.font = font;
    self.textLabelTwo.textColor = textColor;
}

-(void)setAnimateDurationTime:(NSInteger)duration{
    self.duration = duration;
}



#pragma mark - Action

-(void)repeadFuntion{
    
    if (self.index >=self.dataArray.count) {
        self.index = 0;
    }
    if (self.dataArray.count != 0 && self.dataArray != nil) {
        if ([self.dataArray[self.index] isKindOfClass:[NSAttributedString class]]) {
            self.textLabelTwo.attributedText = self.dataArray[self.index];
        }else{
            NSString *text = [NSString stringWithFormat:@"%@",self.dataArray[self.index]];
            self.textLabelTwo.text = text;
        }
    }

    UILabel *oneLabel = self.textLabelOne;
    UILabel *twoLabel = self.textLabelTwo;

    oneLabel.frame = CGRectMake(0, 0, self.width, self.height);
    twoLabel.frame = CGRectMake(0, self.height, self.width, self.height);
    
    // 设置默认
    if (self.duration == 0) {
        self.duration = 1.0;
    }
    
    [UIView animateWithDuration:self.duration animations:^{
        oneLabel.frame = CGRectMake(0, -self.height, self.width, self.height);
        twoLabel.frame = CGRectMake(0, 0, self.width, self.height);
    } completion:^(BOOL finished) {
        self.textLabelOne = twoLabel;
        self.textLabelTwo = oneLabel;
    }];
    self.index ++;
}


#pragma mark - set、get

-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
}


-(UILabel *)textLabelOne{
    if (_textLabelOne == nil) {
        _textLabelOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _textLabelOne.textAlignment = NSTextAlignmentLeft;
    }
    return _textLabelOne;
}

-(UILabel *)textLabelTwo{
    if (_textLabelTwo == nil) {
        _textLabelTwo = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _textLabelTwo.textAlignment = NSTextAlignmentLeft;
    }
    return _textLabelTwo;
}












@end
